﻿using System.Configuration;
using Fly.FlyCommon;
using System;
using System.Xml;
using RN.ERP.DTO;
using System.Collections.Generic;
using System.Web;
using System.ServiceModel.Activation;

namespace RN.ERP
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    internal class Utilities
    {
        internal static DbType GetDbType()
        {
            string area = ConfigurationManager.AppSettings["Area"];
            switch (area)
            {
                case "02":
                    return DbType.QD;
                case "00":
                case "05":
                case "06":
                case "10":
                    return DbType.Zbdlry;
                case "07":
                    return DbType.WF;
                case "14":
                    return DbType.LC;
                default:
                    return DbType.Zbdlry;
            }
        }

        internal static List<ExamSetting> GetExamXMLSettings()
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "LoadFiles\\ExamXML.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            XmlNode theExam = doc.DocumentElement;
            XmlNodeList examNodeList = theExam.SelectNodes("//ExamSettings//ExamSetting");
            List<string> verify = new List<string>();
            List<string> verify2 = new List<string>();
            List<ExamSetting> examList = new List<ExamSetting>();
            foreach (XmlNode en in examNodeList)
            {
                verify2.Clear();
                ExamSetting exam = new ExamSetting();
                exam.Title = en.Attributes["Title"].Value;
                exam.Code = en.Attributes["Code"].Value;
                if (string.IsNullOrEmpty(exam.Title) || string.IsNullOrEmpty(exam.Code) || verify.Contains(exam.Code.ToLower()))
                {
                    continue;
                }
                else
                {
                    verify.Add(exam.Code.ToLower());
                    List<DataPermissions> DPList = new List<DataPermissions>();
                    XmlNodeList datalist = en.ChildNodes;
                    foreach (XmlNode dn in datalist)
                    {
                        DataPermissions dp = new DataPermissions();
                        dp.RoleId = dn.Attributes["RoleId"].Value;
                        if (string.IsNullOrEmpty(dp.RoleId) || verify.Contains(dp.RoleId))
                        {
                            continue;
                        }
                        else
                        {
                            verify2.Add(dp.RoleId);
                            List<Permissions> permission = new List<Permissions>();
                            XmlNodeList perlist = dn.ChildNodes;
                            foreach (XmlNode pn in perlist)
                            {
                                Permissions per = new Permissions();
                                per.Sql = pn.Attributes["Sql"].Value != null ? pn.Attributes["Sql"].Value : "";
                                per.Page = pn.Attributes["Page"].Value != null ? pn.Attributes["Page"].Value : "";
                                permission.Add(per);
                            }
                            dp.Permission = permission;
                            DPList.Add(dp);
                        }
                    }
                    exam.DP = DPList;
                    examList.Add(exam);
                }
            }
            return examList;
        }

        /// <summary>
        /// 从缓存中获取考试模块相关信息
        /// </summary>
        internal string GetExamPagePermission(string code, string roleId, string page)
        {
            //if (HttpContext.Current.Application[code.ToLower()] != null)
            //{
            //    ExamSetting exam = (ExamSetting)HttpContext.Current.Application[code.ToLower()];
            //    foreach (var item in exam.DP)
            //    {
            //        if (item.RoleId == roleId)
            //        {
            //            foreach (var dp in item.Permission)
            //            {
            //                if (dp.Page.ToLower() == page.ToLower())
            //                {
            //                    return dp.Sql;
            //                }
            //            }
            //        }
            //    }
            //}

            if (HttpRuntime.Cache[code.ToLower()] == null)
            {
                SetExamPagePermission();
            }

            if (HttpRuntime.Cache[code.ToLower()] != null)
            {
                ExamSetting exam = (ExamSetting)HttpRuntime.Cache[code.ToLower()];
                foreach (var item in exam.DP)
                {
                    if (item.RoleId == roleId)
                    {
                        foreach (var dp in item.Permission)
                        {
                            if (dp.Page.ToLower() == page.ToLower())
                            {
                                return dp.Sql;
                            }
                        }
                    }
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 将考试模块相关信息放到缓存中
        /// </summary>
        internal static void SetExamPagePermission()
        {
            List<ExamSetting> examList = GetExamXMLSettings();
            foreach (ExamSetting item in examList)
            {
                HttpRuntime.Cache[item.Code.ToLower()] = item;
                //if (Application.Get(item.Code.ToLower()) == null)
                //{
                //    Application[item.Code.ToLower()] = item;
                //}
            }
        }
    }
}
